<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Api\IndexApi;
use App\Http\Controllers\Api\IndexLazyloadApi;
use App\Http\Controllers\Home\ShowLinksController;
use App\Model\Goods;
use App\Model\Goodstype;
use App\Http\Controllers\Api\SearchApi;

/**
 * [前台商品列表页控制器]
 * @author [wt1ao] <[424356452@qq.com]>
 */
class HomeGoodsList extends Controller
{
    /**
     * [index 商品列表页]
     * @param  Request $request [搜索条件]
     * @return [view]
     */
    public function index(Request $request)
    {

        $index = new IndexApi();
        //获取站点信息
      	$sate = IndexApi::getSate();
        //获取所有分类信息
        $types = $index->getAllTypes();

        //查询友情链接数据
        $link = new ShowLinksController;
        $linksArray = $link->index();

        //实例化搜索类
        $search = new SearchApi('abc');
        //查询热搜词
        $HotSearch = $search->getHot();

        //获取当前分类的分类名
        if (!empty($request->id)) {

          $typeName = Goodstype::select('type_name')->where('id', $request->id)->first()->toArray();

        }

        //查询所有品牌信息
        $lazy = new IndexLazyloadApi;
        $brands = $lazy->getBrands();

        //定义默认排序条件，销售量正序
        $order = 'buy_num';
        $sort = 'asc';

        //判断是否搜索品牌
        if ( !empty($request->brand_id) ) {
            $where[] = ['brand_id', $request->brand_id];
            $brand = $request->id;
        } else {
            $brand = '';
        }

        //判断是否有根据销售量排序
        if ( !empty($request->buy_num) ) {
          $order = 'buy_num';
          $sort = $request->buy_num;
          $buy_num = $request->buy_num;
        } else {
          $buy_num = '';
        }

        //判断是否有根据上架时间排序
        if ( !empty($request->created_at) ) {
          $order = 'created_at';
          $sort = $request->created_at;
          $created_at = $request->created_at;
        } else {
          $created_at = '';
        }

        //判断是否有根据价格排序
        if ( !empty($request->price) ) {
          $order = 'price';
          $sort = $request->price;
          $price = $request->price;
        } else {
          $price = '';
        }

        //判断是否有搜索价格区间
        if ( !empty($request->p1) && empty($request->p2) ) {

            $where[] = ['price', '>', $request->p1];
            $p1 = $request->p1;
            $p2 = '';

        } elseif( empty($request->p1) && !empty($request->p2) ) {

            $where[] = ['price', '<', $request->p2];
            $p1 = '';
            $p2 = $request->p2;

        } elseif( !empty($request->p1) || $request->p1 == '0' && !empty($request->p2) || $request->p2 == '0'){

            $where[] = ['price', '>=', $request->p1];
            $where[] = ['price', '<=', $request->p2];
            $p1 = $request->p1;
            $p2 = $request->p2;

        } else {
            $p1 = '';
            $p2 = '';
        };

        //判断是否有子分类
        $sons = Goodstype::select('id')
              ->where('pid', $request->id)
              ->get()
              ->toArray();
        if($sons) {

            //如果有子类，就把子类下所有商品搜出来
            $arr = array_column($sons, 'id');

            $where[] = ['status', 0];

            $goods = Goods::select('id', 'goods_name', 'price', 'goods_pic', 'buy_num')->where($where)->whereIn('type_id', $arr)->orderBy($order, $sort)->paginate(8);

        } else if(empty($request->id)) {

            $where[] = ['status', 0];

            $goods = Goods::select('id', 'goods_name', 'price', 'goods_pic', 'buy_num')->where($where)->orderBy($order, $sort)->paginate(8);

        } else {
            //如果没子类，就搜自己
            $where[] = ['type_id', $request->id];
            $where[] = ['status', 0];

            $goods = Goods::select('id', 'goods_name', 'price', 'goods_pic', 'buy_num')->where($where)->orderBy($order, $sort)->paginate(8);
        }


      	return view('Home.goods-list', [
                              'sate' => $sate,
                              'linksArray' => $linksArray,
                              'HotSearch' => $HotSearch,
                              'goods' => $goods,
                              'types' => $types,
                              'typeName' => $typeName['type_name'],
                              'brands' => $brands,
                              'brand_id' => $brand,
                              'type_id' => $request->id,
                              'buy_num' => $buy_num,
                              'price' => $price,
                              'created_at' => $created_at,
                              'p1' => $p1,
                              'p2' => $p2
                              ]);
    }
}
